{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8e1e876b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "from jieba import del_word, add_word"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "559f29a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Dumping model to file cache C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 1.743 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "全模式分词结果: 我/ 来到/ 北京/ 清华/ 清华大学/ 华大/ 大学\n"
     ]
    }
   ],
   "source": [
    "# 全模式，把句子中所有的可以成词的词语都扫描出来, 速度非常快，但是不能解决歧义；\n",
    "seg_list = jieba.cut('我来到北京清华大学', cut_all=True)\n",
    "print('全模式分词结果: ' + '/ '.join(seg_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a19549cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精确模式分词结果: 我/ 来到/ 北京/ 清华大学\n"
     ]
    }
   ],
   "source": [
    "# 精确模式，试图将句子最精确地切开，适合文本分析；\n",
    "seg_list = jieba.cut('我来到北京清华大学', cut_all=False)\n",
    "print('精确模式分词结果: ' + '/ '.join(seg_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1dec3396",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "他, 来到, 了, 网易, 杭研, 大厦\n"
     ]
    }
   ],
   "source": [
    "seg_list = jieba.cut('他来到了网易杭研大厦')\n",
    "print(', '.join(seg_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6561ac8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "搜索引擎模式分词结果: 小明, 硕士, 毕业, 于, 中国, 科学, 学院, 科学院, 中国科学院, 计算, 计算所, ，, 后, 在, 日本, 京都, 大学, 日本京都大学, 深造\n"
     ]
    }
   ],
   "source": [
    "# 搜索引擎模式，在精确模式的基础上，对长词再次切分，提高召回率，适合用于搜索引擎分词。\n",
    "seg_list = jieba.cut_for_search('小明硕士毕业于中国科学院计算所，后在日本京都大学深造')\n",
    "print('搜索引擎模式分词结果: ' + ', '.join(seg_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0bc4cb97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 载入词典 载入文件格式一个词占一行，每一行分三部分：词语、词频（可省略）、词性（可省略），用空格隔开，顺序不可颠倒，词频省略时使用自动计算的能保证分出该词的词频。\n",
    "jieba.load_userdict('vocabularity.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a13dd5ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 add_word(word, freq=None, tag=None) 和 del_word(word) 可在程序中动态修改词典\n",
    "# del_word('杭研大厦')\n",
    "add_word('杭研大厦')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "69893bc7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "载入词典分词结果: 他/ 来到/ 了/ 网易/ 杭研大厦\n"
     ]
    }
   ],
   "source": [
    "# 比如医疗，旅游中有一些词分不出来，可以加大这些词的权重，这样就可以分出来了\n",
    "seg_list = jieba.cut('他来到了网易杭研大厦', cut_all=False)\n",
    "print('载入词典分词结果: ' + '/ '.join(seg_list))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
